package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 141. 环形链表
 * @date 2025/1/21 13:30
 */
public class E141 {

    /**
     * <h3>方法一：快慢指针</h3>
     *
     * @param head 头结点
     * @return 是否有环
     */
    public boolean hasCycle(ListNode head) {
        ListNode rabbit = head;
        ListNode tortoise = head;
        while (rabbit != null && tortoise != null && rabbit.next != null) {
            rabbit = rabbit.next.next;
            tortoise = tortoise.next;
            if (rabbit == tortoise) return true;
        }
        return false;
    }
}
